package dfbz.com.controller;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    public BaseServlet() {
        super();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        String uri = req.getRequestURI().replace(req.getContextPath(), "");
//        System.out.println(uri);

        String[] split =uri.split("/");

        Class<? extends BaseServlet> servletClass = this.getClass();

        //通过反射，找到那一层的Servlet
        String className = servletClass.getName();
        try {
            Class<?> aClass = Class.forName(className);
            //实例化那个Servlet
            Object obj = aClass.newInstance();  //相当于实力化（new一个出来）

            String methodName = split[2];
            //通过反射，找到方法，并用代理去执行他
            Method method = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(obj,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
